import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] arr = {1,2,3,4,5,6,7,8};
        System.out.println("请输入你要查找的数:");
        int num = input.nextInt();
        int index = fun(arr,num);
        if(-1 == index) {
            System.out.println("没有找到该数的下标");
        }else {
            System.out.println(index + " : 是该数的下标.");
        }

    }
    public static int fun(int[] arr, int num) {
        int head = 0;
        int end = arr.length - 1;
        int mid = 0;
        while (head <= end) {
            mid = head + (end - head)/2;
            if (arr[mid] == num) {
                return mid;
            } else if (arr[mid] < num) {
                head = mid + 1;
            } else if(arr[mid] > num) {
                end = mid - 1;
            }
        }
        return -1;



    }
}
